package com.web.portal.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.portal.pojo.LabelArticle;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.LabelArticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章标签中间表
 */
@Slf4j
@RestController
@Api(tags = "文章标签中间表管理")
@RequestMapping("/admin/label_artice")
public class LabelArticeAdminApi {

    @Autowired
    private LabelArticeService labelArticeService;

    /**
     * 添加文章标签中间表
     * 需要管理员权限
     *
     * @param labelArticle
     * @return
     */
    @PostMapping
    public ResponseResult addLabelArtice(@RequestBody LabelArticle labelArticle) {
        boolean flag = labelArticeService.save(labelArticle);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }

    /**
     * 获取 文章标签中间
     * <p>
     * 使用的case:修改的时候，获取一下。填充弹窗
     * 不获取也是可以的，从列表里获取数据
     * <p>
     * 权限：管理员权限
     *
     * @param labelArticeId
     * @return
     */
    @ApiOperation(value = "用 id 获取文章标签中间")
    @GetMapping("find_label_artice/{label_artice_id}")
    public ResponseResult getLabelArtice(@PathVariable("label_artice_id") String labelArticeId) {
        LabelArticle labelArticle = labelArticeService.getById(labelArticeId);
        if (labelArticle == null) {
            return ResponseResult.FAILED("查找失败");
        }
        return ResponseResult.SUCCESS().setData(labelArticle);
    }

    // 查询带分页
    @ApiOperation(value = "分类查询带分页")
    @PostMapping("find_label_artice/{current}/{limit}")
    public ResponseResult findLabelArtice(@PathVariable long current,
                                          @PathVariable long limit) {
        //创建page对象，传递当前页，每页记录数
        Page<LabelArticle> page = new Page<>(current, limit);
        //调用方法实现分页查询
        QueryWrapper<LabelArticle> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        Page<LabelArticle> pageLabelArtice = labelArticeService.page(page);
        //返回结果
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(pageLabelArtice);
    }

    /**
     * 获取文章标签中间列表
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取文章标签中间列表")
    public ResponseResult listLabelArtice() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(labelArticeService.list());
    }

    /**
     * 更新 文章标签中间
     *
     * @param labelArticle
     * @return
     */
    @PostMapping("update_label_artice")
    @ApiOperation(value = "更改文章标签中间")
    public ResponseResult updateLabelArtice(@RequestBody LabelArticle labelArticle) {
        boolean flag = labelArticeService.updateById(labelArticle);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    /**
     * 文章标签中间
     * @param labelArticeId
     * @return
     */
    @DeleteMapping("/{labelArticeId}")
    @ApiOperation(value = "删除标签中间")
    public ResponseResult deleteLabelArtice(@PathVariable("labelArticeId") String labelArticeId) {
        boolean flag = labelArticeService.removeById(labelArticeId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }
}
